﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common.Service;
using System.Collections;
using OC.Common.Entity;
using System.Text;

namespace OC
{
    public partial class OrganizationStructure : System.Web.UI.Page
    {
        OC.Common.Interface.IDictionary _dictionaryService;

        public OC.Common.Interface.IDictionary DictionaryService
        {
            get
            {
                if (_dictionaryService == null)
                {
                    _dictionaryService = ServiceContainer.GetService<OC.Common.Interface.IDictionary>();
                }
                return _dictionaryService;
            }
            set { _dictionaryService = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitStructureView();
            }
        }

        private void InitStructureView()
        {
            IList structure = DictionaryService.GetListByDictionaryType("DictionaryLogic_GetListByDictionaryType", "ORGANIZATION");
            
            foreach (DictionaryEntity item in structure)
            {
                if (item.PARENT_DICTIONARY_ID.Trim() == "0")//先找到根节点
                {
                    TreeNode tn = new TreeNode();
                    tn.Text = item.CN_NAME;
                    tn.Value = item.DICTIONARY_ID;
                    GetChildNodeByList(structure, item.DICTIONARY_ID, tn);
                    tvStructureView.Nodes.Add(tn);
                    break;
                }
            }
            tvStructureView.ExpandDepth = 1;
        }

        /// <summary>
        /// 根据ID在查询到的组织数据中轮询查找它的子节点
        /// </summary>
        /// <param name="structure"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        private TreeNode GetChildNodeByList(IList structure,string id,TreeNode ptn)
        {
            foreach (DictionaryEntity item in structure)
            {
                if (id == item.PARENT_DICTIONARY_ID)
                {
                    TreeNode tn = new TreeNode();
                    tn.Text = item.CN_NAME;
                    tn.Value = item.DICTIONARY_ID;
                    ptn.ChildNodes.Add(tn);
                    foreach (DictionaryEntity citem in structure)
                    {
                        if (item.DICTIONARY_ID == citem.PARENT_DICTIONARY_ID)
                        {
                            TreeNode ctn = new TreeNode();
                            ctn.Text = citem.CN_NAME;
                            ctn.Value = citem.DICTIONARY_ID;
                            tn.ChildNodes.Add(ctn);
                        }
                    }
                }
            }
            return ptn;
        }

        protected void tvStructureView_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
        {
            TreeNode tn = e.Node;
            if (tn.ChildNodes.Count > 0)
            {
                foreach (TreeNode item in tn.ChildNodes)
                {
                    if (item.ChildNodes.Count <= 0)
                    {
                        IList dicEntitys = DictionaryService.GetList(item.Value);
                        foreach (DictionaryEntity dics in dicEntitys)
                        {
                            TreeNode ctn = new TreeNode();
                            ctn.Text = dics.CN_NAME;
                            ctn.Value = dics.DICTIONARY_ID;
                            item.ChildNodes.Add(ctn);
                        }
                    }
                }
            }
        }
       
        protected void tvStructureView_SelectedNodeChanged(object sender, EventArgs e)
        {
            TreeNode tn = tvStructureView.SelectedNode;
            DictionaryEntity dicEntity = DictionaryService.Get(tn.Value);
            if (dicEntity!= null)
            {
                txtOrganizationCHName.Text = tn.Text;
                txtOrganizationENName.Text = dicEntity.EN_NAME;
                txtRemarks.Text = dicEntity.EXT15;
                txtSortNum.Text = dicEntity.ORDER_NO.ToString();
                txtUpOrganization.Text = tn.Parent != null ? tn.Parent.Text : string.Empty;
                txtNum.Text = dicEntity.DICTIONARY_CODE;
            }
        }

        protected void btnQueryUser_Click(object sender, EventArgs e)
        {
            string userName = txtUserName.Text;
            bool isChilds = cbIdDisplayChilds.Checked;

            Dictionary<string, List<string>> dic = DictionaryService.GetAllList("ORGANIZATION");
            IList userList = null;
            if (isChilds)
            {
                userList = DictionaryService.GetUserByDictionaryID(dic[tvStructureView.SelectedValue], userName);
            }
            else
            {
                List<string> dicID = new List<string>();
                dicID.Add(tvStructureView.SelectedValue);
                userList = DictionaryService.GetUserByDictionaryID(dicID, userName);
            }
            rptUserList.DataSource = userList;
            rptUserList.DataBind();
        }
    }
}